<template>
    <div>
        <el-row>
            <el-col :span="6">
                <el-input
                    size="medium"
                    placeholder="请输入要搜索的关键字"
                    v-model="searchText"
                >
                    <el-button slot="append" icon="el-icon-search" @click="searchChange"></el-button>
                </el-input>
                <div>
                    <ul class="infinite-list">
                        <li 
                            v-for="(item, index) in address"
                            :key="index"
                            @click="geocoder(item.location)"
                            class="infinite-list-item"
                        >
                            <div>
                                <img class="img" src="//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png" alt="">
                            </div>
                            <div>
                                <p class="title">{{ item.name }}</p>
                                <p class="address">{{ item.district + item.address}}</p>
                            </div>
                        </li>
                    </ul>
                </div>
            </el-col>
            <el-col :span="18">
                <div id="AMap"></div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
export default {
    props: {
        
    },
    data() {
        return {
            aMap: null,
            searchText: '',
            selectInfo: {},
            address: [],
            markers: []
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        init() {
            this.aMap = new AMap.Map("AMap", {
                resizeEnable: true
            })
            this.aMap.on('click', this.clickMap)
        },
        /* 监听输入框 */
        searchChange() {
            let autoComplete = new AMap.Autocomplete({
                city: '全国'
            })
            autoComplete.search(this.searchText, (status, result) => {
                if (status === 'complete') {
                    console.log(status, '搜索结果', result)
                    this.address = result.tips
                    this.addMarker()
                }
            })
        },
        /* 添加标记点
        * 点击地图传参，搜索不传参
        */
        addMarker(locations = []) {
            if (this.markers.length && !locations.length) this.removeMarker()

            let add = locations.length ? locations : this.address

            add.forEach(({ location: { lng, lat } }) => {
                // 注意高德 输入返回的数据中有可能 经纬度为 undefined，在创建marker时传入undefined会报错
                let marker = new AMap.Marker({
                    map: this.aMap,
                    icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                    position:[lng ? lng : 0, lat ? lat : 0],
                    offset: new AMap.Pixel(-13, -30)
                })
                /* 监听marker点击事件 */
                marker.on('click', (e) => this.geocoder(e.lnglat))
                /* 存储marker */
                this.markers.push(marker)
            })

            !locations.length && this.fitView()
        },
        /* 点击事件 */
        clickMap(e) {
            console.log('点击事件',e)
            console.log('marker and address length', this.markers.length, this.address.length)
            if (this.markers.length > this.address.length) this.removeMarker(this.markers[this.markers.length-1])
            this.addMarker([{ location: e.lnglat }])
            this.geocoder(e.lnglat)
        },
        /* 显示信息 */
        showInfo({ lng, lat }, addressInfo) {
            new AMap.InfoWindow({
                anchor: 'top-left',
                content: addressInfo.regeocode.formattedAddress
            }).open(this.aMap, [lng, lat])
        },
        /* 获取经纬度位置信息 */
        geocoder({ lng, lat }) {
            new AMap.Geocoder().getAddress([lng, lat], (status, result) => {
                if (status === 'complete' && result.info === 'OK') {
                    console.log('查询地址信息', result)
                    this.showInfo({ lng, lat }, result)
                }
            })
            this.aMap.setCenter([lng, lat])

        },
        /* 自适应点标记 */
        fitView() {
            this.aMap.setFitView()
        },
        /* 清空所有标记点 */
        removeMarker(marker) {
            console.log('remove')
            this.aMap.remove(marker ? marker : this.markers)
            if (marker) this.markers.shift()
            else this.markers = []
        }
    },
};
</script>
<style >
.amap-icon img{
            width: 25px;
            height: 34px;
        }
</style>

<style scoped>
 
#AMap{
    width: 100%;
    height: 500px
}
.search-input {
    width: 200px;
}
.infinite-list{
    height: 450px;
    overflow: auto;
    margin: 0;
    padding: 0;
}
.infinite-list img{
    width: 20px;
    margin-right: 15px;
}
.infinite-list-item{
    display: flex;
    padding: 10px;
    list-style: none;
    border-bottom: 1px solid #ccc;
}
.infinite-list-item:hover{
    display: flex;
    list-style: none;
    background: #eee;
}
.infinite-list-item p{
    margin: 0;
    line-height: 30px;
}
.infinite-list-item .title{
    font-weight: 700;
    font-size: 14px;
}
.infinite-list-item .address{
    font-size: 12px;
    color: #666;
}
</style>
